package com.coocaa.launcher.settings.defaultsetting;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.net.Uri;

import com.coocaa.x.framework.utils.LogUtils;

public class DefaultLauncherSetting {
    private static DefaultLauncherSetting instance = null;
    private Context mContext = null;
    private DefaultSettingBack mDefaultSettingBack = null;
    private static final String PKG = "com.tianci.appstore";

    public static DefaultLauncherSetting getInstance(Context c) {
        if (instance == null) {
            instance = new DefaultLauncherSetting(c);
        }
        return instance;
    }

    private DefaultLauncherSetting(Context c) {
        mContext = c;
    }

    public void destory() {
        mDefaultSettingBack = null;
    }

    public void setDefaultSettingBack(DefaultSettingBack back) {
        mDefaultSettingBack = back;
    }

    private static String getLauncherPackageName(Context context) {
        final Intent intent = new Intent(Intent.ACTION_MAIN);
        intent.addCategory(Intent.CATEGORY_HOME);
        final ResolveInfo res = context.getPackageManager().resolveActivity(intent, 0);
        if (res.activityInfo == null) {
            // should not happen. A home is always installed, isn't it?
            return null;
        }
        if (res.activityInfo.packageName.equals("android")) {
            // 有多个桌面程序存在，且未指定默认项时；
            return null;
        } else {
            return res.activityInfo.packageName;
        }
    }

    public String getDefaultLauncher() {
        String pkg = getLauncherPackageName(mContext);
        if (pkg != null && !pkg.equals("")) {
            return pkg;
        }
        return "";
    }

    private void showSelectLauncherDialog() {
        Intent intent = new Intent(Intent.ACTION_MAIN);
        intent.addCategory(Intent.CATEGORY_HOME);
        intent.addCategory(Intent.CATEGORY_DEFAULT);
        mContext.startActivity(intent);
    }

    private void openApplicationDetail(String pkg) {
        LogUtils.d("default", "openApplicationDetail!!!!!");
        try {
            Intent i = new Intent("android.settings.APPLICATION_DETAILS_SETTINGS");

            String spkg = "com.android.settings";
            String cls = "com.android.settings.applications.InstalledAppDetails";

            i.setComponent(new ComponentName(spkg, cls));
            i.setData(Uri.parse("package:" + pkg));
            mContext.startActivity(i);
        } catch (Exception e) {
            LogUtils.d("exception", "open settings failed !!!!!!!!!  e:" + e);
            mDefaultSettingBack.onOpenAppDetailFailed();
        }
    }

    public void clearDefaultSetting(String pkg) {
        LogUtils.d("default", "clearDefaultSetting!!!!!" + pkg);
        try {
            PackageManager pm = mContext.getPackageManager();
            pm.clearPackagePreferredActivities(pkg);
            mDefaultSettingBack.onClearDefaultSetting();
        } catch (Exception e) {
            LogUtils.d("exception", "clearDefaultSetting failed !!!!!!!!!  e:" + e);
            mDefaultSettingBack.onClearDefaultSettingFailed();
        }
    }

    public void setDefaultLauncher() {
        String pkg = getDefaultLauncher();
        LogUtils.d("default", "setDefaultLauncher!!!!! pkg:" + pkg);
        if (pkg.equals("")) {
            showSelectLauncherDialog();
        } else if (!pkg.equals("") && !pkg.equals(PKG)) {
            openApplicationDetail(pkg);
        }
    }

    public boolean getDefaultLauncherStatus() {
        String pkg = getDefaultLauncher();
        LogUtils.d("default", "setDefaultLauncher!!!!! pkg:" + pkg);
        if (pkg.equals("")) {
            mDefaultSettingBack.onBackStatus(false);
            return false;
        } else if (!pkg.equals("") && !pkg.equals(PKG)) {
            mDefaultSettingBack.onBackStatus(false);
            return false;
        } else {
            mDefaultSettingBack.onBackStatus(true);
            return true;
        }
    }
}
